package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.equipmentCommand.*;
import com.woniu.outlet.po.Equipment;
import com.woniu.service.EquipmentService;
import com.woniu.util.ResponseResult;
import com.woniu.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "设备管理")
@RequestMapping("/equipment/")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    //查询设备列表
    @GetMapping("findEquipments")
    @ApiOperation("查询全部设备信息")
    public ResponseResult<List<Equipment>> findEquipments() throws Exception{
        List<Equipment> equipments = equipmentService.findEquipments();
        return new ResponseResult<>(equipments, "OK", ResultCode.SUCCESS.getCode());
    }

    //查询设备(根据id)
    @GetMapping("findEquipmentById")
    @ApiOperation("查询设备信息(根据设备id)")
    public ResponseResult<Equipment> findEquipmentById(Long id) throws Exception{
        Equipment equipment = equipmentService.findEquipmentById(id);
        return new ResponseResult<>(equipment,"OK", ResultCode.SUCCESS.getCode());
    }

    //添加设备
    @PostMapping("addEquipment")
    @ApiOperation("添加设备信息")
    @ApiOperationSupport(ignoreParameters = {"id","equipmentNo","machineNo","equipmentStatus","buyDate",
            "spare1","spare2","spare3"})
    public ResponseResult<String> addEquipment(@Valid Equipment equipment){
        Command command = new AddEquipmentCommand();
        command.excute(equipment);
        return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"添加设备成功");
    }

    //修改设备
    @PostMapping("updateEquipment")
    @ApiOperation("修改设备信息")
    @ApiOperationSupport(ignoreParameters = {"equipmentNo","machineNo","buyDate",
            "spare1","spare2","spare3"})
    public ResponseResult<String> updateEquipment(Equipment equipment){
        Command command = new UpdateEquipmentCommand();
        boolean flag = (boolean) command.excute(equipment);
        if (flag){
            return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"修改设备信息成功");
        }
        return new ResponseResult<>(ResultCode.ID_IS_NOT_EXIST.getCode(),ResultCode.ID_IS_NOT_EXIST.getMessage(),"修改设备信息失败");
    }

    //删除设备
    @DeleteMapping("deleteEquipmentById")
    @ApiOperation("删除设备信息(根据设备id)")
    public ResponseResult<String> deleteEquipmentById(Long id){
        Command command = new DeleteEquipmentCommand();
        boolean flag = (boolean) command.excute(id);
        if (flag){
            return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"删除设备成功");
        }
        return new ResponseResult<>(ResultCode.ID_IS_NOT_EXIST.getCode(),ResultCode.ID_IS_NOT_EXIST.getMessage(),"删除设备信息失败");
    }


}
